#include "staffwindow.h"
#include "ui_staffwindow.h"
#include "staffform1.h"
#include"staffform2.h"
#include<QCompleter>
staffWindow::staffWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::staffWindow)
{
    ui->setupUi(this);
}
staffWindow::staffWindow(const QString& name,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::staffWindow)
{
    ui->setupUi(this);

    QList<QString> wordlist;
    wordlist<<"10000"<<"10001"<<"10002"<<"10003"<<"10004"<<"10005"<<"10006"<<"10007"<<"10008"<<"10009"<<"10010"
              <<"10011"<<"10012"<<"10013"<<"10014";
    QCompleter *completer=new QCompleter(wordlist,this);
    ui->goodsNumEdit->setCompleter(completer);
    timer=new QTimer;
    saleLog=new QFile("./data/saleLog.txt");

    ui->staffLabel->setText(name);
    ui->dayLabel->setText(QDate::currentDate().toString("yyyy-MM-dd"));
    connect(timer,&QTimer::timeout,this,&staffWindow::showTime);
    timer->start(1000);

}



staffWindow::~staffWindow()
{
    delete ui;
}

void staffWindow::DeleteItem(const QString& name)
{

    for(int i=0;i<saledName.size();i++)
    {
        qDebug()<<saledName[i];
        if(saledName[i]==name)
        {
            qDebug()<<"找到"<<saledName[i];
            ui->goodsChooselistWidget->takeItem(i);
            saledName.removeAt(i);
            break;
        }
    }

}

void staffWindow::on_confirmNumButton_2_clicked()
{


    QFile *goodsData=new QFile("./data/goods.json");
    if(!goodsData->open(QIODevice::ReadOnly))
    {
        qDebug()<<"open file failed ";
    }

   arr1=goodsData->readAll();


    goodsData->close();

    QJsonDocument doc=QJsonDocument::fromJson(arr1);

    QJsonObject object1= doc.object();

    QJsonValue value1=object1.value("Goods");
    qDebug()<< value1.toString();
    QJsonArray array1=  value1.toArray();

    qDebug()<<array1.size();

    bool goodsFound=false;

    QListWidgetItem *aitem1=new QListWidgetItem;
    aitem1->setSizeHint(QSize(391,117));

    for(int i=0;i<array1.size();i++)
    {
       if(ui->goodsNumEdit->text()==array1.at(i).toObject().value("编号").toString())
       {
           goodsFound=true;
           saledName<<array1.at(i).toObject().value("名字").toString();
           ui->goodsChooselistWidget->addItem(aitem1);
           staffForm1 *Form1=new  staffForm1(array1.at(i).toObject().value("编号").toString(),array1.at(i).toObject().value("名字").toString(),
           array1.at(i).toObject().value("售卖单价").toDouble(),array1.at(i).toObject().value("图片位置").toString(),this);
           ui->goodsChooselistWidget->setItemWidget(aitem1,Form1);
       }



    }

    if(!goodsFound)
    {
        QMessageBox::critical(this,"查找错误","无此货物，重新输入编号");
    }


}

void staffWindow::on_countButton_clicked()
{

    if(!saleLog->open(QIODevice::WriteOnly|QIODevice::Append))
    {
         qDebug()<<"open file failed ";
    }

    QJsonDocument doc=QJsonDocument::fromJson(arr1);

    QJsonObject object1= doc.object();
    QJsonValue value1=object1.value("Goods");
    QJsonArray array1=  value1.toArray();

    QListWidgetItem *aitem2=new QListWidgetItem;
    aitem2->setSizeHint(QSize(281,22));
    ui->purchasingListWidget->addItem(aitem2);

    staffForm2 *Form2=new staffForm2;
    ui->purchasingListWidget->setItemWidget(aitem2,Form2);
    double totalCost=0;
    for(int j=0;j<saledName.size();j++)
    {
        for(int i=0;i<array1.size();i++)
        {
            if(saledName[j]==array1.at(i).toObject().value("名字").toString())
            {
                QListWidgetItem *aitem3=new QListWidgetItem;
                aitem3->setSizeHint(QSize(281,22));
                 ui->purchasingListWidget->addItem(aitem3);
                staffForm2 *Form3=new staffForm2(array1.at(i).toObject().value("名字").toString(),"1",array1.at(i).toObject().value("售卖单价").toDouble());
                ui->purchasingListWidget->setItemWidget(aitem3,Form3);

                qDebug()<<array1.at(i).toObject().value("售卖单价").toInt();
                totalCost+=array1.at(i).toObject().value("售卖单价").toDouble();


                QJsonObject tmp= array1.at(i).toObject();



                tmp.insert("编号",array1.at(i).toObject().value("编号").toString());
                tmp.insert("名字",array1.at(i).toObject().value("名字").toString());
                tmp.insert("成本",array1.at(i).toObject().value("成本").toDouble());
                tmp.insert("售卖单价",array1.at(i).toObject().value("售卖单价").toDouble());
                tmp.insert("卖出数量",array1.at(i).toObject().value("卖出数量").toInt()+1);
                tmp.insert("库存",array1.at(i).toObject().value("库存").toInt()-1);
                tmp.insert("图片位置",array1.at(i).toObject().value("图片位置").toString());

                array1.removeAt(i);
                array1.insert(i,tmp);

                object1.insert("Goods",array1);



                qDebug()<<"名字"<<array1.at(i).toObject().value("名字").toString();
               qDebug()<<"卖出数量: "<<array1.at(i).toObject().value("卖出数量").toInt();
                qDebug()<<"库存: "<<array1.at(i).toObject().value("库存").toInt();

                saleLog->write( QByteArray(QString(ui->dayLabel->text()+"|"+ui->timeLabel->text()+"|"+array1.at(i).toObject().value("名字").toString()+"|"+
                  QString::number(array1.at(i).toObject().value("售卖单价").toDouble(),10,2)+"|"+"1"+"|"+QString::number(array1.at(i).toObject().value("成本").toDouble(),10,2)+"|"+
                                                  ui->staffLabel->text()+"|"+QString::number(array1.at(i).toObject().value("库存").toInt(),10)).toUtf8()+"\r\n"));

                break;

            }

        }
    }

    doc.setObject(object1);
    QByteArray change=doc.toJson();

    QFile *goodsData=new QFile("./data/goods.json");
    if(!goodsData->open(QIODevice::WriteOnly|QIODevice::Truncate))
    {
        qDebug()<<"open file failed ";
    }

    goodsData->write(change);
    goodsData->close();
    saleLog->close();
    ui->toTalpriceLabel->setNum(totalCost);


}

void staffWindow::showTime()
{

    ui->timeLabel->setText(QTime::currentTime().toString("hh:mm:ss"));
}
